const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    avatarUrl: '',
    //往日训练总数
    trainDayCount: [],
    trainPlan: [],
    currentDay: '',
    //当前显示的对象信息
    trainInfo: {
      trainNote: '',
      courseTrains: [],
      trainTypes: [],
      trainMovements: [],
      bodyMeasurements: [],
      pictures: []
    },

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const phone = wx.getStorageSync('phone');
    const avatarUrl = wx.getStorageSync('avatarUrl');
    this.setData({
      phone,
      avatarUrl
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that = this;
    const phone = wx.getStorageSync('phone');
    const url = app.globalData.url;
    //提取今天的训练数据
    wx.request({
      url: url + '/train/trainDayListByPhone/' + phone,
      success: function(response) {
        if (response.data.success) {
          const trainDayRecords = response.data.content;
          const currentDay = trainDayRecords[trainDayRecords.length - 1];
          that.setData({
            currentDay: currentDay,
            trainDayCount: trainDayRecords
          });
          //提取最后一天的训练
          wx.request({
            url: url + '/train/dailyTrainByPhone',
            data: {
              phone,
              trainDate: currentDay
            },
            method: 'POST',
            success: function(response) {
              if (response.data.success) {
                const dailyTrain = response.data.content;
                that.transferData(dailyTrain);
              }
            }
          });
        }
      }
    })
  },
  changeDay: function(e) {
    const that = this;
    const currentIndex = e.detail.current;
    const trainDayCount = that.data.trainDayCount;
    const selectDay = trainDayCount[currentIndex];
    const url = app.globalData.url;
    const phone = that.data.phone;
    that.setData({
      currentDay: selectDay
    });
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function() {
      wx.hideLoading();
      wx.request({
        url: url + '/train/dailyTrainByPhone',
        data: {
          phone,
          trainDate: selectDay
        },
        method: 'POST',
        success: function(response) {
          if (response.data.success) {
            const dailyTrain = response.data.content;
            that.transferData(dailyTrain);
          }
        }
      });
    }, 1000);
  },

  transferData: function(dailyTrain) {
    const bodyMeasurements = dailyTrain.bodyMeasurements;
    if (bodyMeasurements && bodyMeasurements.length > 0) {
      bodyMeasurements.forEach(function(item) {
        const dislayName = item.name + '(' + item.unit + ')';
        const dislayName2 = dislayName.length > 6 ? (dislayName.substring(0, 3) + '...') : dislayName;
        item.dislayName = dislayName2;
      });
    }
    this.setData({
      trainInfo: dailyTrain
    });
  },
  toCourseTrain: function() {
    wx.setStorageSync("traineeId", this.data.trainInfo.id);
    wx.navigateTo({
      url: '/pages/courseTrain/courseTrain',
    });
  },
  toTrainArchives:function(){
    const trainArchives = this.data.trainInfo.trainArchives;
    wx.setStorageSync("trainId", this.data.trainInfo.id);
    wx.setStorageSync("trainArchives", JSON.stringify(trainArchives));
    wx.navigateTo({
      url: '/pages/trainArchives/trainArchives',
    })
  },
  toTrainType: function() {
    const trainTypes = this.data.trainInfo.trainTypes;
    const currentDay = this.data.currentDay;
    wx.setStorageSync("trainTypes", JSON.stringify(trainTypes));
    wx.setStorageSync("trainId", this.data.trainInfo.id);
    wx.navigateTo({
      url: '/pages/trainType/trainType',
    });
  },
  toMeasurement: function() {
    const bodyMeasurements = this.data.trainInfo.bodyMeasurements;
    const currentDay = this.data.currentDay;
    wx.setStorageSync("bodyMeasurements", JSON.stringify(bodyMeasurements));
    wx.setStorageSync("trainId", this.data.trainInfo.id);
    wx.navigateTo({
      url: '/pages/bodyMeasurement/bodyMeasurement',
    });
  }
})